<?php

class Message{
	protected static $messages = array();

	/**
	 *  add message to stack, will be rendered by template later 
	 *
	 *  @param string type of message (error, etc)
	 *  @param string message
	 */
	public static function msg( $type, $msg ){
		self::$messages[] = array( 'type'=>$type, 'msg'=>$msg );
	}

	/**
	 *  Add error message
	 *
	 *  @param string error message
	 */
	public static function error( $msg ){
		self::msg( 'error', "Error: $msg" );
	}

	/**
	 *  Add warning message
	 *
	 *  @param string message
	 */
	public static function warn( $msg ){
		self::msg( 'warn', "Warning: $msg" );
	}

	/**
	 *  Add normal status message
	 *
	 *  @param string message
	 */
	public static function status( $msg ){
		self::msg( 'status', $msg );
	}

	public static function render(){
		ob_start();

		foreach( self::$messages as $m ){
			switch( $m['type'] ){
				case 'error':
					$class = 'msg-error';
				break;
				case 'warn':
					$class = 'msg-warn';
				break;
				case 'status':
				case 'ok':
					$class = 'msg-ok';
				break;
			}
			echo "<p class='msg $class'>{$m['msg']}</p>";
		}

		$output = ob_get_contents();
		ob_end_clean();
		return $output;
	}
}
